package kr.school.service;

import java.util.Scanner;

import kr.school.dao.InterfaceDAO;

public abstract class ServiceItem<T> {
	protected InterfaceDAO<T> dao;
	protected Scanner sc;
	public ServiceItem(InterfaceDAO<T> dao, Scanner sc) {
		this.dao = dao;
		this.sc = sc;
		init();
	}
	
	public abstract T inputItemInfo(boolean gubun);
	public abstract void prnSubMenu();
	
	protected void init() {
		dao.loadFile();
		prnMenu();
	}
	
	protected void close() {
		dao.saveFile();
	}
	
	protected void prnMenu() {
		T item = null;
		int res = 0;
		
		do {
			System.out.println("1.입력\n2.수정\n3.삭제\n4.검색\n5.리스트보기\n6.종료");
		 	res = sc.nextInt();
		 	switch(res) {
		 		case 1:	//삽입
		 			item = inputItemInfo(true);
		 			dao.insertItem(item);
		 			break;
		 		case 2:	//수정
		 			dao.prnListItem(null);
		 			System.out.println("수정할 정보를 입력하세요");
		 			item = inputItemInfo(true);
		 			dao.updateItem(item);
		 			break;
		 		case 3:	//삭제
		 			dao.prnListItem(null);
		 			System.out.println("삭제할 정보를 입력하세요");
		 			item = inputItemInfo(false);
		 			dao.deleteItem(item);
		 			break;
		 		case 4:	//검색
		 			System.out.println("검색할 정보를 입력하세요");
		 			item = inputItemInfo(false);
		 			try {
		 				System.out.println(dao.searchItem(item));
		 			} catch(ArrayIndexOutOfBoundsException e) {
		 				System.out.println("해당 정보는 존재하지 않습니다.");
		 			}
		 			
		 			break;
		 		case 5:	//리스트보기
		 			prnSubMenu();
		 			break;
		 		default:
		 			close();
		 			res = 7;
		 	}
		} while(res <= 6);
	}
}